package com.isunland.managesystem.ui;

import android.content.Intent;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.adapter.MessageSendListAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.Message;
import com.isunland.managesystem.entity.MessageSendOriginal;
import com.isunland.managesystem.utils.LogUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 已发送消息
 *
 * @author wangyanan
 */
public class MessageSendListFragment extends BaseListFragment {

  protected static final String TAG = "SuccessMassageFragment";

  private ArrayList<Message> mMessageSendList;
  private MessageSendListAdapter mAdapter;

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    Message message = mMessageSendList.get(position - 1);

    Intent i = new Intent(getActivity(), MessageDetailPagerActivity.class);
    i.putExtra(MessageDetailFragment.EXTRA_MESSAGE, message);
    i.putExtra(MessageDetailFragment.EXTRA_TYPE, MessageDetailFragment.VALUE_SEND);
    startActivity(i);
  }

  @Override public String getUrl() {
    return ApiConst.URL_MASSAGE_SENDlIST;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<String, String>();
    params.put("curPage", "1");
    params.put("pageSize", "1000");
    return params;
  }

  @Override public void resolveResponse(String arg0) {

    MessageSendOriginal successMassageOriginal =
        new Gson().fromJson(arg0, MessageSendOriginal.class);

    List<Message> successMessage = successMassageOriginal.getRows();

    setAdapter(successMessage);

    LogUtil.i(TAG, "successmessage=" + successMessage.size());
  }

  private void setAdapter(List<Message> successMessage) {
    if (mMessageSendList == null) {
      mMessageSendList = new ArrayList<Message>();
    }
    mMessageSendList.clear();
    mMessageSendList.addAll(successMessage);
    if (mAdapter == null) {
      mAdapter = new MessageSendListAdapter(getActivity(), mMessageSendList);
      setListAdapter(mAdapter);
    }
    ((MessageSendListAdapter) getListAdapter()).notifyDataSetChanged();
  }

  public void refresh() {
    volleyPost();
  }
}
